#include "check.h"
#include "common.h"

union adaptor_paramter_all adaptor_parm;

struct operating_state       operate_parm;
struct reportOperating_state operate_parm_report;

union adaptor_paramter_all               flash_ada_parm;
struct configurable_adaptor_paramter_all configurable_ada_parm;

/*1ms延迟函数*/
Function_State_Typedef Delay_1ms(volatile unsigned int i)
{
    volatile unsigned int j;
    while (i--) {
        for (j = 0; j < 8000; j++)
            ;
    }
    return Success;
}

void clear_adaptor_rsvd(union adaptor_paramter_all *adaptor)
{
    // 主激光1参数
    // adaptor->part.laser_module_adaptor_parm.ver1 = SOFT_VER_1;
    // adaptor->part.laser_module_adaptor_parm.ver2 = SOFT_VER_2;
    adaptor->part.laser_module_adaptor_parm.serialNM[0] = 0;
    adaptor->part.laser_module_adaptor_parm.serialNM[1] = 0;
    adaptor->part.laser_module_adaptor_parm.serialNM[2] = 0;
    adaptor->part.laser_module_adaptor_parm.serialNM[3] = 0;
    adaptor->part.laser_module_adaptor_parm.can_id      = DEVICE_ID;
}

void init_ada_parm_and_store(union adaptor_paramter_all *p)
{
    p->part.BCP_Data = Backups_Data;

    // 主激光1参数
    // p->part.laser_module_adaptor_parm.ver1 = 0;
    // p->part.laser_module_adaptor_parm.ver2 = 0;
    p->part.laser_module_adaptor_parm.can_id = DEVICE_ID;

    // p->part.laser_module_adaptor_parm.check_feature_1.all = 0xff;
    // p->part.laser_module_adaptor_parm.check_feature_2.all = 0xFD;
    p->part.laser_module_adaptor_parm.check_feature_1.part.brake      = 1;
    p->part.laser_module_adaptor_parm.check_feature_1.part.power_comm = 1;
    p->part.laser_module_adaptor_parm.check_feature_1.part.t1         = 1;
    p->part.laser_module_adaptor_parm.check_feature_1.part.t2         = 1;
    p->part.laser_module_adaptor_parm.check_feature_1.part.t3         = 0;
    p->part.laser_module_adaptor_parm.check_feature_1.part.t4         = 1;
    p->part.laser_module_adaptor_parm.check_feature_1.part.pd1        = 0;
    p->part.laser_module_adaptor_parm.check_feature_1.part.pd2        = 1;

    p->part.laser_module_adaptor_parm.check_feature_2.part.pd3      = 1;
    p->part.laser_module_adaptor_parm.check_feature_2.part.env_temp = 1;
    p->part.laser_module_adaptor_parm.check_feature_2.part.env_hum  = 1;
    p->part.laser_module_adaptor_parm.check_feature_2.part.mo       = 1;
    p->part.laser_module_adaptor_parm.check_feature_2.part.fa       = 1;
    p->part.laser_module_adaptor_parm.check_feature_2.part.reserve  = 1;
    p->part.laser_module_adaptor_parm.check_feature_2.part.reserve0 = 1;
    p->part.laser_module_adaptor_parm.check_feature_2.part.reserve1 = 1;

    p->part.laser_module_adaptor_parm.pd1_low_limit      = 13;
    p->part.laser_module_adaptor_parm.pd2_low_limit      = 165;
    p->part.laser_module_adaptor_parm.pd3_low_limit      = 3;
    p->part.laser_module_adaptor_parm.t1_up_limit        = 35 * 2;
    p->part.laser_module_adaptor_parm.t1_low_limit       = 15 * 2;
    p->part.laser_module_adaptor_parm.t2_up_limit        = 35 * 2;
    p->part.laser_module_adaptor_parm.t2_low_limit       = 15 * 2;
    p->part.laser_module_adaptor_parm.t3_up_limit        = 35 * 2;
    p->part.laser_module_adaptor_parm.t3_low_limit       = -35 * 2;
    p->part.laser_module_adaptor_parm.t4_up_limit        = 35 * 2;
    p->part.laser_module_adaptor_parm.t4_low_limit       = 15 * 2;
    p->part.laser_module_adaptor_parm.env_temp_up_limit  = 50 * 2;
    p->part.laser_module_adaptor_parm.env_temp_low_limit = 0 * 2;
    p->part.laser_module_adaptor_parm.env_hum_up_limit   = 95;

    p->part.power_output_adaptor_parm.level1_modul1   = 40;  // 1级弱光模块1电流
    p->part.power_output_adaptor_parm.level1_modul2   = 0;   // 1级弱光模块2电流
    p->part.power_output_adaptor_parm.level1_modul3_H = 0;   // 1级弱光模块3电流
    p->part.power_output_adaptor_parm.level1_modul3_L = 0;   // 1级弱光模块3电流
    p->part.power_output_adaptor_parm.level1_modul4_H = 0;   // 1级弱光模块4电流
    p->part.power_output_adaptor_parm.level1_modul4_L = 0;   // 1级弱光模块4电流

    p->part.power_output_adaptor_parm.level2_modul1   = 40;  // 2级弱光模块1电流
    p->part.power_output_adaptor_parm.level2_modul2   = 40;  // 2级弱光模块2电流
    p->part.power_output_adaptor_parm.level2_modul3_H = 0;   // 2级弱光模块3电流
    p->part.power_output_adaptor_parm.level2_modul3_L = 0;   // 2级弱光模块3电流
    p->part.power_output_adaptor_parm.level2_modul4_H = 0;   // 2级弱光模块4电流
    p->part.power_output_adaptor_parm.level2_modul4_L = 0;   // 2级弱光模块4电流

    p->part.power_output_adaptor_parm.strong_modul1   = 40;            // 强光模块1电流
    p->part.power_output_adaptor_parm.strong_modul2   = 120;           // 强光模块2电流
    p->part.power_output_adaptor_parm.strong_modul3_H = (320 >> 8);    // 强光模块3电流
    p->part.power_output_adaptor_parm.strong_modul3_L = (320 & 0xff);  // 强光模块3电流
    p->part.power_output_adaptor_parm.strong_modul4_H = (320 >> 8);    // 强光模块4电流
    p->part.power_output_adaptor_parm.strong_modul4_L = (320 & 0xff);  // 强光模块4电流

    p->part.power_output_adaptor_parm.level1PreCur_up_limit = 50;   // 一级预放电流阈值上限
    p->part.power_output_adaptor_parm.level2PreCur_up_limit = 130;  // 二级预放电流阈值上限
    p->part.power_output_adaptor_parm.fa_up_limit           = 330;  // 主放上限电流阈值

    clear_adaptor_rsvd(p);
    flash_write_halfword((unsigned short *)WRITE_START_ADDR, (unsigned short *)p, sizeof(*p) / 2);
}

void delay_time(volatile unsigned int times)
{
    while (times--)
        ;
}

void init_main_var(void)
{
    memory_set((unsigned char *)&operate_parm, 0, sizeof(operate_parm));
    memory_set((unsigned char *)&operate_parm_report, 0, sizeof(operate_parm_report));
    memory_set((unsigned char *)&configurable_ada_parm, 0, sizeof(configurable_ada_parm));
}

ULONG time_span(ULONG start_time)
{
    return tx_time_get() - start_time;
}

VOID tx_application_define(VOID *first_unused_memory)
{
    /**驱动源查询**/
    tx_thread_create(&power_parm_query_thread_handle, NULL, power_parm_query_thread, 0, power_parm_query_thread_stack, sizeof(power_parm_query_thread_stack), 1, 0, TX_NO_TIME_SLICE, TX_AUTO_START);
    /**驱动源查询**/
    tx_thread_create(&usart_rx_thread_handle, NULL, usart_rx_thread, 0, usart_rx_thread_stack, sizeof(usart_rx_thread_stack), 1, 0, TX_NO_TIME_SLICE, TX_AUTO_START);

    tx_thread_create(&can1_rx0_thread_handle, NULL, can1_rx0_thread, 0, can1_rx0_thread_stack, sizeof(can1_rx0_thread_stack), 1, 0, TX_NO_TIME_SLICE, TX_AUTO_START);

    tx_thread_create(&can1_rx1_thread_handle, NULL, can1_rx1_thread, 0, can1_rx1_thread_stack, sizeof(can1_rx1_thread_stack), 1, 0, TX_NO_TIME_SLICE, TX_AUTO_START);

    tx_thread_create(&pd_success_check_thread_handle, NULL, pd_success_check_thread, 0, pd_success_check_thread_stack, sizeof(pd_success_check_thread_stack), 1, 0, TX_NO_TIME_SLICE, TX_AUTO_START);

    tx_thread_create(&pd1_check_thread_handle, NULL, pd1_check_thread, 0, pd1_check_thread_stack, sizeof(pd1_check_thread_stack), 1, 0, TX_NO_TIME_SLICE, TX_AUTO_START);

    tx_thread_create(&pd2_check_thread_handle, NULL, pd2_check_thread, 0, pd2_check_thread_stack, sizeof(pd2_check_thread_stack), 1, 0, TX_NO_TIME_SLICE, TX_AUTO_START);

    tx_thread_create(&pump_pd_check_thread_handle, NULL, pump_pd_check_thread, 0, pump_pd_check_thread_stack, sizeof(pump_pd_check_thread_stack), 1, 0, TX_NO_TIME_SLICE, TX_AUTO_START);

    tx_thread_create(&state_manage_thread_handle, NULL, state_manage_thread, 0, state_manage_thread_stack, sizeof(state_manage_thread_stack), 1, 0, TX_NO_TIME_SLICE, TX_AUTO_START);

    tx_thread_create(&upload_operate_parm_thread_handle, NULL, upload_operate_parm_thread, 0, upload_operate_parm_thread_stack, sizeof(upload_operate_parm_thread_stack), 1, 0, TX_NO_TIME_SLICE,
                     TX_AUTO_START);

    tx_timer_activate(&cycle_check_timer);
}

/*参数初始化函数*/
void Parameter_Init(void)
{
    init_main_var();
    init_can_app_var();
    init_check_var();
    init_adc_app_var();
    init_usart_app_var();
    init_dma_var();
}

void System_Config(void)
{
    init_flash();
    init_rcc();
    Flash_Config();    // Flash初始化
    Parameter_Init();  // 全局变量初始化
    GPIO_Config();     // GPIO初始化
    init_can();        // CAN总线初始化
    init_adc1();       // ADC1初始化
    init_dma1();
    init_general_timer4();
    initUsart1(72, 115200);
    // init_usart1(); // 串口初始化

    Delay_1ms(700);  // 延时0.7S，防止开机电压不稳导致的中断误触发
    init_extiLine2();

#if (DEVICE_ID == 3)
    init_extiLine12();
#endif
    // init_iwdg();
}

int main(void)
{
    System_Config();
    tx_kernel_enter();
    while (1)
        ;
}

void memory_set(void *dst, unsigned char data, unsigned int size)
{
    unsigned char *p = dst;
    for (unsigned int i = 0; i < size; i++)
        p[i] = data;
}

void memory_copy(void *dst, void *src, unsigned int len)
{
    unsigned char *d = dst;
    unsigned char *s = src;
    for (unsigned int i = 0; i < len; i++) {
        *d = *s;
        d++;
        s++;
    }
}
